package com.briup.estore.adminAPI.web.controller;

import com.briup.estore.common.bean.Category;
import com.briup.estore.common.bean.VO.CategoryVO;
import com.briup.estore.common.service.ICategoryService;
import com.briup.estore.common.util.Result;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author adam
 * @date 2022/10/13
 */
@RestController
@Api(tags = "分类管理")
@RequestMapping("/category")
@CrossOrigin
public class CategoryController {
    @Autowired
    private ICategoryService categoryService;

    @GetMapping("findCategoryInfo")
    public Result findCategoryInfo() {
        try {
            List<CategoryVO> list = categoryService.findAll();
            return Result.success(list);
        } catch (Exception e) {
            return Result.error(80000, e.getMessage());
        }
    }

    @GetMapping("findCategorysOneLevel")
    public Result findCategoryOneLevel() {
        try {
            List<Category> list = categoryService.findOneLevel();
            return Result.success(list);
        } catch (Exception e) {
            return Result.error(80000, e.getMessage());
        }
    }

    @GetMapping("findCategoryByOneLevel/{id}")
    public Result findCategoryByOneLevel(@PathVariable Long id) {
        try {
            List<Category> categories = categoryService.findTwoLevel(id);
            return Result.success(categories);
        } catch (Exception e) {
            return Result.error(80000, e.getMessage());
        }

    }

    @PostMapping("addCategory")
    public Result addCategory(@RequestBody Category category) {
        System.out.println("category = " + category);
        try {
            categoryService.addCategory(category);
            return Result.success();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(50000, e.getMessage());
        }
    }

    @GetMapping("findOneLevelByName")
    public Result findOneLevelByName(String name) {
        List<Category> list = categoryService.findOneLevelByName(name);
        return Result.success(list);
    }

    @GetMapping("findTwoLevelByName")
    public Result findTwoLevelByName(String name, Long parentId) {
        List<Category> list = categoryService.findTwoLevelByName(name, parentId);
        return Result.success(list);
    }

    @DeleteMapping("{id}")
    public Result delete(@PathVariable Long id) {
        categoryService.delete(id);
        return Result.success();

    }

}
